# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# http://www.sdxlp.cn/tool/wechatdat
#
#
# 异或加密
# jpg FF D8
# png 89 50
# gif 47 49
#
#
# func Format(filePath string) byte {
# 	buf, _ := ioutil.ReadFile(filePath)
# 	imgbytes := [3][]byte{
# 		{0xff, 0xd8},
# 		{0x89, 0x50},
# 		{0x47, 0x49},
# 	}
# 	for _, xor := range imgbytes {
# 		if xor[0]^buf[0] == xor[1]^buf[1] {
# 			return byte(xor[0] ^ buf[0])
# 		}
# 	}
# 	return byte(0)
# }
#
#


import os
import tkinter as tk
from tkinter import filedialog


def main():
    # 实例化
    root = tk.Tk()
    root.withdraw()

    # 获取文件夹路径
    indir = outdir = filedialog.askdirectory()
    # indir = filedialog.askopenfilename()  # 文件
    print('\n获取的文件地址：', indir)

    if len(indir) == 0:
        return

    # indir = r'F:\tes'
    outdir = os.getcwd()

    # **文件夹下所有文件
    infiles = os.listdir(indir)

    # 计数成功的文件个数
    count = 0

    # 循环每个文件进行判断、转换
    for infile in infiles:
        # **文件名
        filename = infile[0:infile.find('.')]

        # 二进制读取文件
        f1 = open(os.path.join(indir, infile), 'rb')
        infilebytes = f1.read()
        newfile = []

        # 判断图片类型JPG，通过异或判定
        if (infilebytes[0] ^ 0xFF) == (infilebytes[1] ^ 0xD8):
            y1 = infilebytes[0] ^ 0xFF
            print('%s,文件是JPG图片，每个字节是根据0x%X异或进行加密' % (infile, y1))

            # 字节进行异或转换，组合成新的文件
            for i in infilebytes:
                newbyte = i ^ y1
                newfile.append(newbyte)
            newfile2 = bytes(newfile)

            # 写入新文件
            f2 = open(os.path.join(outdir, filename + '.jpg'), 'wb')
            f2.write(newfile2)
            count += 1

        # 判断图片类型PNG，通过异或判定
        elif (infilebytes[0] ^ 0x89) == (infilebytes[1] ^ 0x50):
            y1 = infilebytes[0] ^ 0x89
            print('%s,文件是PNG图片，每个字节是根据0x%X异或进行加密' % (infile, y1))
            for i in infilebytes:
                newbyte = i ^ y1
                newfile.append(newbyte)
            newfile2 = bytes(newfile)
            f2 = open(os.path.join(outdir, filename + '.png'), 'wb')
            f2.write(newfile2)
            count += 1

        # 判断图片类型GIF，通过异或判定
        elif (infilebytes[0] ^ 0x47) == (infilebytes[1] ^ 0x49):
            y1 = infilebytes[0] ^ 0x47
            print('%s,文件是GIF图片，每个字节是根据0x%X异或进行加密' % (infile, y1))
            for i in infilebytes:
                newbyte = i ^ y1
                newfile.append(newbyte)
            newfile2 = bytes(newfile)
            f2 = open(os.path.join(outdir, filename + '.gif'), 'wb')
            f2.write(newfile2)
            count += 1
        else:
            print('%s无法识别的类型！' % infile)
    print('识别出图片%d张' % count)

    os.system("pause")


#
# dat = fopen('D:\CTF备战-GG\Bugku\Misc\FileStoragedat\keli.dat', 'rb');
# % 需要解码的dat文件
#
# A = fread(dat); % 将dat文件的数据读取出来
# A = uint8(A);
# B = bitxor(A, uint8(158));
# % 此处158就是解密字节码的十进制数值，需要更换为自己的解密码
#
# png = fopen('D:\CTF备战-GG\Bugku\Misc\FileStoragedat\Test2.png', 'wb');
# % 将处理后的数据写入png格式文件，解密成功
#
# fwrite(png, B);
# fclose(png);
# fclose(dat); % 将上述打开的文档关闭掉
#

if __name__ == '__main__':
    main()
